package com.xuanwei.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuanwei.core.response.ResultVO;
import com.xuanwei.core.service.IXwAdManagerService;
import com.xuanwei.dao.entity.XwAdManager;
import org.hibernate.validator.constraints.URL;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;

/**
 * @Author jiangyong
 * @Description TODO
 * @Date 2019/8/1 10:29
 **/
@RestController
@RequestMapping("api/adManager")
@Validated
public class AdManagerController extends BasicController {

    private IXwAdManagerService adManagerService;

    public AdManagerController(IXwAdManagerService adManagerService) {
        this.adManagerService = adManagerService;
    }

    @RequestMapping(value = "add.do", method = RequestMethod.GET)
    public ResultVO add(Integer id,
                        @URL(message = "[image]格式错误") String image,
                        @NotBlank(message = "[name]不能为空") String name,
                        @NotBlank(message = "[url]格式错误") String url,
                        @Min(value = 1, message = "[position]格式错误") Integer position,
                        @Min(value = 0, message = "[orderBy]格式错误") Integer orderBy,
                        @Min(value = 0, message = "[showStatus]格式错误") Integer showStatus) {
        adManagerService.addOrUpdate(id, image, name, url, position, orderBy, showStatus);
        return ResultVO.successResult("保存成功");
    }

    @RequestMapping(value = "changeStatus.do", method = RequestMethod.GET)
    public ResultVO changeStatus(@Min(value = 1, message = "[ID]格式错误") Integer id) {
        adManagerService.updateStatus(id);
        return ResultVO.successResult("修改成功");
    }

    @RequestMapping(value = "delete.do", method = RequestMethod.GET)
    public ResultVO delete(@Min(value = 1, message = "[ID]格式错误") Integer id) {
        adManagerService.delete(id);
        return ResultVO.successResult("删除成功");
    }


    @RequestMapping(value = "list.do", method = RequestMethod.GET)
    public ResultVO list(Integer position) {
        Page<XwAdManager> page = getPage();
        adManagerService.findPage(position, page);
        return ResultVO.successResult(page);
    }

}
